<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/h_something/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/h_something/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/h_something/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/h_something/images/logo.svg" color="#222">

<link rel="stylesheet" href="/h_something/css/main.css">


<link rel="stylesheet" href="/h_something/lib/font-awesome/css/font-awesome.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"ddota2.gitee.io","root":"/h_something/","scheme":"Muse","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="楼下的男人病的要死">
<meta property="og:type" content="website">
<meta property="og:title" content="D">
<meta property="og:url" content="https://ddota2.gitee.io/index.html">
<meta property="og:site_name" content="D">
<meta property="og:description" content="楼下的男人病的要死">
<meta property="og:locale" content="zh_CN">
<meta property="article:author" content="Dota2 player">
<meta property="article:tag" content="D">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://ddota2.gitee.io/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : true,
    isPost : false,
    lang   : 'zh-CN'
  };
</script>

  <title>D</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/h_something/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">D</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">而已</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/h_something/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/h_something/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/h_something/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/h_something/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content index posts-expand">
            
      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://ddota2.gitee.io/2021/01/11/%E6%B5%85%E8%B0%88%E6%8B%86%E5%8C%85%E8%A3%85%E5%8C%85/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/h_something/images/avatar.gif">
      <meta itemprop="name" content="Dota2 player">
      <meta itemprop="description" content="楼下的男人病的要死">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="D">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/h_something/2021/01/11/%E6%B5%85%E8%B0%88%E6%8B%86%E5%8C%85%E8%A3%85%E5%8C%85/" class="post-title-link" itemprop="url">浅谈拆包装包</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2021-01-11 15:03:15 / 修改时间：16:54:23" itemprop="dateCreated datePublished" datetime="2021-01-11T15:03:15+08:00">2021-01-11</time>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <p>关于拆包装包这块，以前一直是一知半解，也觉得没有什么需要特别写出来的必要，反正是 JAVA 自带的性质。<br>最近发现里面还是有一些以前不曾在意的机制的，简单说一下。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="keyword">new</span> Integer(<span class="number">1</span>) == <span class="keyword">new</span> Integer(<span class="number">1</span>)); <span class="comment">//1</span></span><br><span class="line">    </span><br><span class="line">        System.out.println(<span class="keyword">new</span> Integer(<span class="number">1</span>) == <span class="number">1</span>); <span class="comment">//2</span></span><br><span class="line">    </span><br><span class="line">        System.out.println(<span class="keyword">new</span> Integer(<span class="number">1</span>) + <span class="keyword">new</span> Integer(<span class="number">1</span>) == <span class="number">2</span>); <span class="comment">//3</span></span><br><span class="line">    </span><br><span class="line">        Integer a = <span class="number">1</span>;</span><br><span class="line">        Integer b = <span class="number">1</span>;</span><br><span class="line">        System.out.println(a==b); <span class="comment">//4</span></span><br><span class="line">    </span><br><span class="line">        Integer a1 = <span class="number">128</span>;</span><br><span class="line">        Integer b1 = <span class="number">128</span>;</span><br><span class="line">        System.out.println(a1==b1); <span class="comment">//5</span></span><br><span class="line">        </span><br><span class="line">        <span class="keyword">int</span> a2 = <span class="keyword">new</span> Integer(<span class="number">1236</span>);</span><br><span class="line">        Integer b2 = <span class="keyword">new</span> Integer(<span class="number">1236</span>);</span><br><span class="line">        System.out.println(a2==b2); <span class="comment">//6</span></span><br><span class="line">   </span><br><span class="line">&#125;</span><br><span class="line">可以得到结果:</span><br><span class="line"><span class="keyword">false</span> <span class="comment">//1</span></span><br><span class="line"><span class="keyword">true</span> <span class="comment">//2</span></span><br><span class="line"><span class="keyword">true</span> <span class="comment">//3</span></span><br><span class="line"><span class="keyword">true</span> <span class="comment">//4</span></span><br><span class="line"><span class="keyword">false</span> <span class="comment">//5</span></span><br></pre></td></tr></table></figure>

<ol>
<li>因为自动装包机制，a 和 b 实际指向的是对象的内存地址。而两个 new 出来的对象地址一定不相等</li>
<li>因为自动拆包机制，当包装类型与基础类型比较时，会进行拆包，对基础类型的值进行比较，所以相等</li>
<li>当包装类型进行计算时，会进行自动拆包，故比较的是值，而不是地址，为true。</li>
<li>因为基础类型的缓存机制，int类型会缓存[-128,127]的值，int a = 1,实际上是类似于调用了 int a = Integet.valueOf(1)，如果缓存中存在，则会直接返回该对象，并不会new，所以同一个对象的内存引用地址一样，故相等</li>
<li>同上，只是128超出了缓存的范围，因此是new出来的，所以两个对象引用地址不同。</li>
<li>声明时是 int， int a2 = new Integer(1236) 实际上是 int a2 = new Integer(1236).intValue();  且当包装类型与基础类型比较时，会自动拆表比较值。</li>
</ol>
<p>根据自身理解，自动拆包有一下几种情况：</p>
<ol>
<li>当存在算术符号时</li>
<li>当包装类型与基础类型比较时</li>
<li>声明对象时使用基础类型声明时</li>
</ol>
<p>自动包装有以下几种情况：</p>
<ol>
<li>声明对象为包装类型时，包括new，和直接给定值。区别在于，new的时候一定是一个新的对象，而后者通常调用了XXX.valueOf(a)，有可能会从缓存中直接获取。<ul>
<li>int a = new Integer(1);</li>
<li>int a = 1;</li>
</ul>
</li>
</ol>
<p><strong>Tips</strong>：</p>
<p>包装类型缓存范围如下：</p>
<table>
<thead>
<tr>
<th>类型</th>
<th align="left">范围</th>
</tr>
</thead>
<tbody><tr>
<td>Byte,Short,Integer,Long</td>
<td align="left">[-128,127]</td>
</tr>
<tr>
<td>Character</td>
<td align="left">[0,127] 前128个字符</td>
</tr>
<tr>
<td>Boolean</td>
<td align="left">true,false</td>
</tr>
<tr>
<td>Float, Double</td>
<td align="left">无缓存</td>
</tr>
</tbody></table>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://ddota2.gitee.io/2020/12/17/docker-es/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/h_something/images/avatar.gif">
      <meta itemprop="name" content="Dota2 player">
      <meta itemprop="description" content="楼下的男人病的要死">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="D">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/h_something/2020/12/17/docker-es/" class="post-title-link" itemprop="url">docker es</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-12-17 15:07:18" itemprop="dateCreated datePublished" datetime="2020-12-17T15:07:18+08:00">2020-12-17</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-12-21 19:05:30" itemprop="dateModified" datetime="2020-12-21T19:05:30+08:00">2020-12-21</time>
              </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <h1 id="环境"><a href="#环境" class="headerlink" title="环境"></a>环境</h1><p>安装 docker 就不多说了</p>
<p><strong>Elasticsearch 以及 ik， kibana 版本一定要一致，此次使用的版本是7.9.0</strong></p>
<h2 id="Elasticsearch"><a href="#Elasticsearch" class="headerlink" title="Elasticsearch"></a>Elasticsearch</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">docker pull elasticsearch:7.9.0</span><br><span class="line">docker run --network=dnetwork  -d -p 9200:9200 -p 9300:9300 -e ES_JAVA_OPTS="-Xms512m -Xmx512m" -e "discovery.type=single-node" --name es elasticsearch:7.9.0</span><br></pre></td></tr></table></figure>
<p>安装成功后,可以看到</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">root@ubuntu:~<span class="comment"># curl  localhost:9200</span></span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">"name"</span> : <span class="string">"8b9d30ea5a68"</span>,</span><br><span class="line">  <span class="string">"cluster_name"</span> : <span class="string">"docker-cluster"</span>,</span><br><span class="line">  <span class="string">"cluster_uuid"</span> : <span class="string">"htfsXjtCTpiLnJhakFKhOw"</span>,</span><br><span class="line">  <span class="string">"version"</span> : &#123;</span><br><span class="line">    <span class="string">"number"</span> : <span class="string">"7.9.0"</span>,</span><br><span class="line">    <span class="string">"build_flavor"</span> : <span class="string">"default"</span>,</span><br><span class="line">    <span class="string">"build_type"</span> : <span class="string">"docker"</span>,</span><br><span class="line">    <span class="string">"build_hash"</span> : <span class="string">"a479a2a7fce0389512d6a9361301708b92dff667"</span>,</span><br><span class="line">    <span class="string">"build_date"</span> : <span class="string">"2020-08-11T21:36:48.204330Z"</span>,</span><br><span class="line">    <span class="string">"build_snapshot"</span> : <span class="literal">false</span>,</span><br><span class="line">    <span class="string">"lucene_version"</span> : <span class="string">"8.6.0"</span>,</span><br><span class="line">    <span class="string">"minimum_wire_compatibility_version"</span> : <span class="string">"6.8.0"</span>,</span><br><span class="line">    <span class="string">"minimum_index_compatibility_version"</span> : <span class="string">"6.0.0-beta1"</span></span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="string">"tagline"</span> : <span class="string">"You Know, for Search"</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="kibana"><a href="#kibana" class="headerlink" title="kibana"></a>kibana</h2><p>kibana 是一个简单的 ES 可视化管理工具。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">docker pull kibana:7.9.0</span><br><span class="line">docker run --network=dnetwork --name kibana --link=elasticsearch:8b9d30ea5a68  -p 5601:5601 -d kibana:7.9.0</span><br></pre></td></tr></table></figure>
<p>安装成功后访问浏览器 localhost:5601 可以看到界面。</p>
<h2 id="ES简单使用"><a href="#ES简单使用" class="headerlink" title="ES简单使用"></a>ES简单使用</h2><h3 id="简单概念"><a href="#简单概念" class="headerlink" title="简单概念"></a>简单概念</h3><p>ElasticSearch的索引即index，可以理解为MySQL的 Table (6.x之前可以理解为database， 6.x之后可理解为table)。因为之前在 index 和文档之间有一层 type(类型)，且可以创建多个mapping，而7.0没有了，一个 index 也只能有一个 mapping。</p>
<h3 id="索引"><a href="#索引" class="headerlink" title="索引"></a>索引</h3><p>索引名必须全小写。<br><strong>创建文档时，也会自动生成索引。</strong></p>
<h4 id="新增索引"><a href="#新增索引" class="headerlink" title="新增索引"></a>新增索引</h4><p>PUT: /{index_name}</p>
<h5 id="path-parameters"><a href="#path-parameters" class="headerlink" title="path parameters:"></a>path parameters:</h5><ul>
<li>index_name:<ul>
<li>全小写</li>
<li>不能包含, /, *, ?, “, &lt;, &gt;, |, <code></code> (空白符), ,, #</li>
<li>7.0 之前可能包含’:’，但是7.0+不支持</li>
<li>不能以 -, _, + 开头</li>
<li>不能为. 或者 ..</li>
<li>不能超过255 byte，一个汉字2个byte</li>
<li>名字不能以 . 开头，因为默认 . 开头的会被隐藏，通常只用于系统插件。<h5 id="query-parameters"><a href="#query-parameters" class="headerlink" title="query parameters:"></a>query parameters:</h5></li>
</ul>
</li>
<li>wait_for_active_shards（可选，字符串）：任意正整数（1 到 number_of_replicas+1）或者all，默认1.等待碎片副本数达到后，执行后续操作。</li>
<li>master_timeout（可选，时间单位(d,h,m,s,ms,micros,nanos)）：指定连接master node 的超时时间，默认30s。</li>
<li>timeout(可选，时间单位(d,h,m,s,ms,micros,nanos))： 指定响应超时时间，默认30s。</li>
</ul>
<h5 id="request-body："><a href="#request-body：" class="headerlink" title="request body："></a>request body：</h5><ul>
<li>aliases（可选）： index 别名，</li>
<li>mappings（可选）： （mapping 对象）如果指定了，可以包含三个属性：<ul>
<li>Field names</li>
<li>Field data types</li>
<li>Mapping parameters</li>
</ul>
</li>
<li>settings(可选): 配置index<br>举个例子:<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">PUT &#x2F;my-index-000001</span><br><span class="line">&#123;</span><br><span class="line">  &quot;settings&quot;: &#123;</span><br><span class="line">    &quot;number_of_shards&quot;: 3,</span><br><span class="line">    &quot;number_of_replicas&quot;: 2,</span><br><span class="line">    &quot;index.write.wait_for_active_shards&quot;: &quot;2&quot;</span><br><span class="line">  &#125;,</span><br><span class="line">  &quot;mappings&quot;: &#123;</span><br><span class="line">    &quot;properties&quot;: &#123;</span><br><span class="line">      &quot;field1&quot;: &#123; &quot;type&quot;: &quot;text&quot; &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">    &quot;aliases&quot;: &#123;</span><br><span class="line">    &quot;alias_1&quot;: &#123;&#125;,</span><br><span class="line">    &quot;alias_2&quot;: &#123;</span><br><span class="line">      &quot;filter&quot;: &#123;</span><br><span class="line">        &quot;term&quot;: &#123; &quot;user.id&quot;: &quot;kimchy&quot; &#125;</span><br><span class="line">      &#125;,</span><br><span class="line">      &quot;routing&quot;: &quot;shard-1&quot;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="删除索引"><a href="#删除索引" class="headerlink" title="删除索引"></a>删除索引</h4>DELETE /{index_name}<h5 id="path-parameters-1"><a href="#path-parameters-1" class="headerlink" title="path parameters:"></a>path parameters:</h5></li>
<li>index_name<ul>
<li>可以逗号分割</li>
<li>可以使用 wildcard 匹配</li>
<li>不能使用 alias。</li>
<li>_all 或者 * 可以删除所有索引。如果要禁止此行为，需要设置 action.destructive_requires_name 为 true。<br>在 elasticsearch.yml 中修改或者使用 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.9/cluster-update-settings.html" target="_blank" rel="noopener">cluster update settings API</a><h5 id="query-parameters-1"><a href="#query-parameters-1" class="headerlink" title="query parameters:"></a>query parameters:</h5></li>
</ul>
</li>
<li>allow_no_indices: 当通配符表达式或者_all， 仅能匹配到关闭的索引时。抛出异常。默认true。</li>
<li>expand_wildcards：all/open/closed/hidden/none, 默认open</li>
<li>ignore_unavailable： 默认true, missing 或者 close 的索引不会在响应中。</li>
<li>master_timeout（可选，时间单位(d,h,m,s,ms,micros,nanos)）：指定连接master node 的超时时间，默认30s。</li>
<li>timeout(可选，时间单位(d,h,m,s,ms,micros,nanos))： 指定响应超时时间，默认30s。<h5 id="查询索引"><a href="#查询索引" class="headerlink" title="查询索引"></a>查询索引</h5>GET: /_cat/indices/?v 可以查看 ES 所有索引，以及他的相关信息，比如占用空间等等。<h5 id="查看单个索引详细信息"><a href="#查看单个索引详细信息" class="headerlink" title="查看单个索引详细信息"></a>查看单个索引详细信息</h5>GET: /{index_name}<h5 id="path-parameters-2"><a href="#path-parameters-2" class="headerlink" title="path parameters:"></a>path parameters:</h5></li>
<li>index_name<ul>
<li>可以逗号分割</li>
<li>可以使用 wildcard 匹配</li>
<li>_all 或者 * 可以删除所有索引。</li>
</ul>
</li>
</ul>
<h5 id="query-parameters-2"><a href="#query-parameters-2" class="headerlink" title="query parameters:"></a>query parameters:</h5><ul>
<li>allow_no_indices<br>当通配符表达式或者_all， 仅能匹配到关闭的索引时。抛出异常。默认true</li>
<li>expand_wildcards：all/open/closed/hidden/none, 默认open</li>
<li>flat_settings: 如果为 true, 以 flat foramt 返回所有 settings。默认为false。</li>
<li>include_defaults： 是否包括默认 settings。默认false</li>
<li>include_type_name： 7.0之后移除，为 true 。一个 mapping 类型则指定一个mapping body。默认false。</li>
<li>ignore_unavailable： 默认true, missing 或者 close 的索引不会在响应中。</li>
<li>local： 如果true， 只会接口本地节点的信息。默认false，也就是从 master 节点接收信息。</li>
<li>master_timeout（可选，时间单位(d,h,m,s,ms,micros,nanos)）：指定连接master node 的超时时间，默认30s。</li>
</ul>
<h3 id="Documents-文档："><a href="#Documents-文档：" class="headerlink" title="Documents 文档："></a>Documents 文档：</h3><p>documents 其实也类似 sql 中的一条数据。index则有点像表，是数据的集合。</p>
<h4 id="新增数据-index"><a href="#新增数据-index" class="headerlink" title="新增数据(index)"></a>新增数据(index)</h4><p>新增数据有如下接口：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">PUT &#x2F;&lt;target&gt;&#x2F;_doc&#x2F;&lt;_id&gt;</span><br><span class="line"></span><br><span class="line">POST &#x2F;&lt;target&gt;&#x2F;_doc&#x2F;</span><br><span class="line"></span><br><span class="line">PUT &#x2F;&lt;target&gt;&#x2F;_create&#x2F;&lt;_id&gt;</span><br><span class="line"></span><br><span class="line">POST &#x2F;&lt;target&gt;&#x2F;_create&#x2F;&lt;_id&gt;</span><br></pre></td></tr></table></figure>
<p>新增时请使用 <code>POST /&lt;target&gt;/_create/&lt;_id&gt;</code>,不要使用<code>PUT /&lt;target&gt;/_doc/&lt;_id&gt;</code>。经过测试已存在的记录的时，前者不会新增，且会响应出错，后者会使其版本号+1。</p>
<h5 id="Path-parameters"><a href="#Path-parameters" class="headerlink" title="Path parameters"></a>Path parameters</h5><ul>
<li>target: 必填，目标数据流或者索引名字。</li>
<li>_id: 非必填，文档ID，以下接口下必填<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">PUT &#x2F;&lt;target&gt;&#x2F;_doc&#x2F;&lt;_id&gt;</span><br><span class="line">PUT &#x2F;&lt;target&gt;&#x2F;_create&#x2F;&lt;_id&gt;</span><br><span class="line">POST &#x2F;&lt;target&gt;&#x2F;_create&#x2F;&lt;_id&gt;</span><br></pre></td></tr></table></figure>
若要自动生成文档ID，使用接口<code>/&lt;target&gt;/_doc/</code>。并忽略此参数<h4 id="Request-body"><a href="#Request-body" class="headerlink" title="Request body"></a>Request body</h4>请求体，也就是文档 _source。 json格式。</li>
</ul>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://ddota2.gitee.io/2020/12/16/ubuntu%E6%9C%8D%E5%8A%A1%E4%BB%A5%E5%8F%8A%E8%87%AA%E5%90%AF%E5%8A%A8/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/h_something/images/avatar.gif">
      <meta itemprop="name" content="Dota2 player">
      <meta itemprop="description" content="楼下的男人病的要死">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="D">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/h_something/2020/12/16/ubuntu%E6%9C%8D%E5%8A%A1%E4%BB%A5%E5%8F%8A%E8%87%AA%E5%90%AF%E5%8A%A8/" class="post-title-link" itemprop="url">ubuntu服务以及自启动</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2020-12-16 16:35:37 / 修改时间：16:36:48" itemprop="dateCreated datePublished" datetime="2020-12-16T16:35:37+08:00">2020-12-16</time>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          
      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://ddota2.gitee.io/2020/06/07/JVM%20%E8%B0%83%E8%AF%95/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/h_something/images/avatar.gif">
      <meta itemprop="name" content="Dota2 player">
      <meta itemprop="description" content="楼下的男人病的要死">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="D">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/h_something/2020/06/07/JVM%20%E8%B0%83%E8%AF%95/" class="post-title-link" itemprop="url">jvm 远程调试参数记录</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-06-07 12:03:09" itemprop="dateCreated datePublished" datetime="2020-06-07T12:03:09+08:00">2020-06-07</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-11-19 16:40:59" itemprop="dateModified" datetime="2020-11-19T16:40:59+08:00">2020-11-19</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/h_something/categories/java/" itemprop="url" rel="index"><span itemprop="name">java</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <p>java -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=18081 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.rmi.port=18081 -Djava.rmi.server.hostname=120.197.152.99 -jar X.jar</p>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://ddota2.gitee.io/2020/06/07/d/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/h_something/images/avatar.gif">
      <meta itemprop="name" content="Dota2 player">
      <meta itemprop="description" content="楼下的男人病的要死">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="D">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/h_something/2020/06/07/d/" class="post-title-link" itemprop="url">d</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-06-07 12:03:09" itemprop="dateCreated datePublished" datetime="2020-06-07T12:03:09+08:00">2020-06-07</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-11-19 16:40:32" itemprop="dateModified" datetime="2020-11-19T16:40:32+08:00">2020-11-19</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/h_something/categories/d/" itemprop="url" rel="index"><span itemprop="name">d</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <p>这种艺术真的很难领悟</p>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://ddota2.gitee.io/2020/06/07/hello-world/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/h_something/images/avatar.gif">
      <meta itemprop="name" content="Dota2 player">
      <meta itemprop="description" content="楼下的男人病的要死">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="D">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/h_something/2020/06/07/hello-world/" class="post-title-link" itemprop="url">Hello World</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-06-07 11:57:53" itemprop="dateCreated datePublished" datetime="2020-06-07T11:57:53+08:00">2020-06-07</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-05-18 21:02:15" itemprop="dateModified" datetime="2020-05-18T21:02:15+08:00">2020-05-18</time>
              </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <p>Welcome to <a href="https://hexo.io/" target="_blank" rel="noopener">Hexo</a>! This is your very first post. Check <a href="https://hexo.io/docs/" target="_blank" rel="noopener">documentation</a> for more info. If you get any problems when using Hexo, you can find the answer in <a href="https://hexo.io/docs/troubleshooting.html" target="_blank" rel="noopener">troubleshooting</a> or you can ask me on <a href="https://github.com/hexojs/hexo/issues" target="_blank" rel="noopener">GitHub</a>.</p>
<h2 id="Quick-Start"><a href="#Quick-Start" class="headerlink" title="Quick Start"></a>Quick Start</h2><h3 id="Create-a-new-post"><a href="#Create-a-new-post" class="headerlink" title="Create a new post"></a>Create a new post</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo new <span class="string">"My New Post"</span></span><br></pre></td></tr></table></figure>

<p>More info: <a href="https://hexo.io/docs/writing.html" target="_blank" rel="noopener">Writing</a></p>
<h3 id="Run-server"><a href="#Run-server" class="headerlink" title="Run server"></a>Run server</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo server</span><br></pre></td></tr></table></figure>

<p>More info: <a href="https://hexo.io/docs/server.html" target="_blank" rel="noopener">Server</a></p>
<h3 id="Generate-static-files"><a href="#Generate-static-files" class="headerlink" title="Generate static files"></a>Generate static files</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo generate</span><br></pre></td></tr></table></figure>

<p>More info: <a href="https://hexo.io/docs/generating.html" target="_blank" rel="noopener">Generating</a></p>
<h3 id="Deploy-to-remote-sites"><a href="#Deploy-to-remote-sites" class="headerlink" title="Deploy to remote sites"></a>Deploy to remote sites</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo deploy</span><br></pre></td></tr></table></figure>

<p>More info: <a href="https://hexo.io/docs/one-command-deployment.html" target="_blank" rel="noopener">Deployment</a></p>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://ddota2.gitee.io/2020/02/15/pg-repmgr/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/h_something/images/avatar.gif">
      <meta itemprop="name" content="Dota2 player">
      <meta itemprop="description" content="楼下的男人病的要死">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="D">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/h_something/2020/02/15/pg-repmgr/" class="post-title-link" itemprop="url">pg_repmgr</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-02-15 10:30:24" itemprop="dateCreated datePublished" datetime="2020-02-15T10:30:24+08:00">2020-02-15</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-11-19 16:51:20" itemprop="dateModified" datetime="2020-11-19T16:51:20+08:00">2020-11-19</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/h_something/categories/postgresql/" itemprop="url" rel="index"><span itemprop="name">postgresql</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <h1 id="repmgr教程"><a href="#repmgr教程" class="headerlink" title="repmgr教程"></a>repmgr教程</h1><h2 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h2><p>repmgr是一个开源工具套件，用于管理PostgreSQL服务器集群中的复制和故障转移。它使用工具来增强PostgreSQL的内置热备份功能，以设置备用服务器，监控复制以及执行管理任务，例如故障转移或手动切换操作。</p>
<h2 id="前期准备"><a href="#前期准备" class="headerlink" title="前期准备"></a>前期准备</h2><p>两台装有 postgresql 9.5 的服务器。</p>
<h2 id="1-安装"><a href="#1-安装" class="headerlink" title="1. 安装"></a>1. 安装</h2><p>1.1 下载源码包<a href="https://repmgr.org/download/repmgr-5.0.0.tar.gz" target="_blank" rel="noopener">https://repmgr.org/download/repmgr-5.0.0.tar.gz</a><br>1.2 解压 </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">tar -zxvf repmgr-5.0.0.tar.gz</span><br></pre></td></tr></table></figure>
<p>1.3 进入目录并编译<br>安装编译所需库</p>
<ul>
<li>flex</li>
<li>libedit-dev</li>
<li>libkrb5-dev</li>
<li>libpam0g-dev</li>
<li>libreadline-dev</li>
<li>libselinux1-dev</li>
<li>libssl-dev</li>
<li>libxml2-dev</li>
<li>libxslt1-dev<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">apt install flex libedit-dev libkrb5-dev libpam0g-dev libreadline-dev libselinux1-dev libssl-dev libxml2-dev libxslt1-dev</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">.&#x2F;configure &amp;&amp; make install</span><br></pre></td></tr></table></figure>
<h2 id="2-配置"><a href="#2-配置" class="headerlink" title="2. 配置"></a>2. 配置</h2>可复制 sample, sample 位于解压的根目录 repmgr.conf.sample。<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cp repmgr.conf.sample &#x2F;etc&#x2F;postgresql&#x2F;repmgr.conf</span><br></pre></td></tr></table></figure>
修改以下关键信息<br>主<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">node_id&#x3D;1</span><br><span class="line">node_name&#x3D;&#39;138&#39;	</span><br><span class="line">conninfo&#x3D;&#39;host&#x3D;db1 user&#x3D;postgres dbname&#x3D;postgres password&#x3D;d connect_timeout&#x3D;2&#39;	</span><br><span class="line">data_directory&#x3D;&#39;&#x2F;var&#x2F;lib&#x2F;postgresql&#x2F;9.5&#x2F;main&#39;</span><br><span class="line">pg_bindir&#x3D;&#39;&#x2F;usr&#x2F;lib&#x2F;postgresql&#x2F;9.5&#x2F;bin&#39;		</span><br><span class="line">repmgr_bindir&#x3D;&#39;&#x2F;usr&#x2F;lib&#x2F;postgresql&#x2F;9.5&#x2F;bin&#39;</span><br></pre></td></tr></table></figure>
从<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">node_id&#x3D;2</span><br><span class="line">node_name&#x3D;&#39;139&#39;	</span><br><span class="line">conninfo&#x3D;&#39;host&#x3D;db2 user&#x3D;postgres dbname&#x3D;postgres password&#x3D;d connect_timeout&#x3D;2&#39;	</span><br><span class="line">data_directory&#x3D;&#39;&#x2F;var&#x2F;lib&#x2F;postgresql&#x2F;9.5&#x2F;main&#39;</span><br><span class="line">pg_bindir&#x3D;&#39;&#x2F;usr&#x2F;lib&#x2F;postgresql&#x2F;9.5&#x2F;bin&#39;		</span><br><span class="line">repmgr_bindir&#x3D;&#39;&#x2F;usr&#x2F;lib&#x2F;postgresql&#x2F;9.5&#x2F;bin&#39;</span><br></pre></td></tr></table></figure>
<h2 id="3-从库复制主库"><a href="#3-从库复制主库" class="headerlink" title="3. 从库复制主库"></a>3. 从库复制主库</h2>主库中创建extension,psql 连接数据库后输入<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">create extension repmgr</span><br></pre></td></tr></table></figure>
创建流复制角色<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">CREATE</span> <span class="keyword">ROLE</span> repl login <span class="keyword">replication</span> encrypted <span class="keyword">password</span> <span class="string">'d'</span></span><br></pre></td></tr></table></figure></li>
</ul>
<p>3.1 修改主库配置<br>postgresql.conf, 和一般的流复制配置无异</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">listen_addresses &#x3D; &#39;*&#39; # 监听来自任何地址的请求</span><br><span class="line">archive_mode &#x3D; on # 开启归档</span><br><span class="line">archive_command &#x3D; &#39;cp %p &#x2F;var&#x2F;lib&#x2F;postgresql&#x2F;pg_archive&#x2F;%f&#39; # 归档 logfile segment 命令 %p 代表需要归档的文件路径，%f 归档文件名，此目录需要属于postgres角色，一面权限不足。</span><br><span class="line">wal_level &#x3D; hot_standby  # 归档开启热备用   minimal, archive, hot_standby, or logical</span><br><span class="line">max_wal_senders &#x3D; 10 # 最大wal发送者数量。通常和从库数量一致。</span><br><span class="line">wal_keep_segments &#x3D; 256 # 保留的最大日志文件段。一个16MB。</span><br><span class="line">wal_sender_timeout &#x3D; 60s # 发送超时时间</span><br><span class="line">max_connections &#x3D; 100 # 这个设置要注意下，从库的max_connections必须要大于主库的。（不知道）。最大连接数。</span><br><span class="line">wal_log_hints &#x3D; on  # 不然会导致主服务器挂掉之后恢复出问题。pg_rewind</span><br><span class="line"># 从库配置</span><br><span class="line">hot_standby &#x3D; on  # 在恢复期间是否允许查询</span><br><span class="line">max_standby_archive_delay &#x3D; 30s # 读取 archive WAL时，取消查询时的最大延迟，-1 无限</span><br><span class="line">#max_standby_streaming_delay &#x3D; 30s # 读取streaming wal 取消查询的最大延迟，-1 无限</span><br><span class="line">wal_receiver_status_interval &#x3D; 10s # 发送回复的间隔</span><br><span class="line">hot_standby_feedback &#x3D; off  # 从 standby 状态发送信息以防止冲突</span><br><span class="line">wal_receiver_timeout &#x3D; 60s # 接收 master 等待的时间，默认 milliseconds</span><br><span class="line">wal_retrieve_retry_interval &#x3D; 5s # 尝试失败后重新检索 WAL 的间隔</span><br></pre></td></tr></table></figure>
<p>pg_hba.conf</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"># local      DATABASE  USER  METHOD  [OPTIONS]</span><br><span class="line"># host       DATABASE  USER  ADDRESS  METHOD  [OPTIONS]</span><br><span class="line">host    replication     repl        0.0.0.0&#x2F;0            md5</span><br><span class="line">host    replication     postgres        0.0.0.0&#x2F;0            md5</span><br></pre></td></tr></table></figure>
<p>3.2 主库注册到 repmgr,并查看</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">repmgr -f &#x2F;etc&#x2F;postgresql&#x2F;repmgr.conf primary register</span><br><span class="line">INFO: connecting to primary database...</span><br><span class="line">NOTICE: attempting to install extension &quot;repmgr&quot;</span><br><span class="line">NOTICE: &quot;repmgr&quot; extension successfully installed</span><br><span class="line">NOTICE: primary node record (ID: 1) registered</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">repmgr cluster show -f &#x2F;etc&#x2F;postgresql&#x2F;repmgr.conf</span><br><span class="line"> ID | Name | Role    | Status    | Upstream | Location | Priority | Connection string</span><br><span class="line">----+------+---------+-----------+----------+----------+----------+---------------------------------------------------------------------</span><br><span class="line"> 1  | 138  | primary | * running |          | default  | 100      | host&#x3D;db1 user&#x3D;postgres dbname&#x3D;postgres password&#x3D;d connect_timeout&#x3D;2</span><br></pre></td></tr></table></figure>
<p>3.3 从库配置<br>将密码连接信息存储在.pgpass文件</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vim ~&#x2F;.pgpass</span><br></pre></td></tr></table></figure>
<p>输入以下信息，格式为 host:port:DBName:username:password</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">db1:5432:postgres:postgres:d</span><br><span class="line">db2:5432:postgres:postgres:d</span><br></pre></td></tr></table></figure>
<p>然后设置权限chmod 600 ~/.pgpass<br>clone 执行，执行前需要清空 pgdata 目录</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">repmgr -h db1 -U postgres -d postgres -f &#x2F;etc&#x2F;postgresql&#x2F;repmgr.conf standby clone</span><br></pre></td></tr></table></figure>
<p>你会发现目录生成了，且 recovery.conf 也生成了。<br>启动从库</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pg_ctl -D . start</span><br></pre></td></tr></table></figure>
<p>然后注册从库</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">repmgr standby register -f &#x2F;etc&#x2F;postgresql&#x2F;repmgr.conf</span><br><span class="line">INFO: connecting to local node &quot;139&quot; (ID: 2)</span><br><span class="line">INFO: connecting to primary database</span><br><span class="line">WARNING: --upstream-node-id not supplied, assuming upstream node is primary (node ID 1)</span><br><span class="line">INFO: standby registration complete</span><br><span class="line">NOTICE: standby node &quot;139&quot; (ID: 2) successfully registered</span><br></pre></td></tr></table></figure>
<p>查看节点</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">repmgr cluster show -f &#x2F;etc&#x2F;postgresql&#x2F;repmgr.conf</span><br><span class="line"> ID | Name | Role    | Status    | Upstream | Location | Priority | Connection string</span><br><span class="line">----+------+---------+-----------+----------+----------+----------+---------------------------------------------------------------------</span><br><span class="line"> 1  | 138  | primary | * running |          | default  | 100      | host&#x3D;db1 user&#x3D;postgres dbname&#x3D;postgres password&#x3D;d connect_timeout&#x3D;2</span><br><span class="line"> 2  | 139  | standby |   running | 138      | default  | 100      | host&#x3D;db2 user&#x3D;postgres dbname&#x3D;postgres password&#x3D;d connect_timeout&#x3D;2</span><br></pre></td></tr></table></figure>

<h2 id="4-配置自动failover-如果使用了-pgpool-failover，则不要使用-repmgr-的failover"><a href="#4-配置自动failover-如果使用了-pgpool-failover，则不要使用-repmgr-的failover" class="headerlink" title="4.配置自动failover(如果使用了 pgpool failover，则不要使用 repmgr 的failover)"></a>4.配置自动failover(如果使用了 pgpool failover，则不要使用 repmgr 的failover)</h2><ol>
<li>因为自动 failover 基于repmgrd，首先需要包含其关联库,postgresql.conf,配置下面一行,需要重启<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">shared_preload_libraries &#x3D;&#39;repmgr&#39;</span><br></pre></td></tr></table></figure>
主从都需要重启:<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pg_ctl -D . restart</span><br></pre></td></tr></table></figure></li>
<li>配置repmgr.conf,配置在上面配置的基础上添加（主从都需要）<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">failover&#x3D;automatic # 开启自动故障转移</span><br><span class="line">promote_command&#x3D;&#39;&#x2F;usr&#x2F;lib&#x2F;postgresql&#x2F;9.5&#x2F;bin&#x2F;repmgr standby promote -f &#x2F;etc&#x2F;postgresql&#x2F;repmgr.conf --log-to-file&#39;</span><br><span class="line">follow_command&#x3D;&#39;&#x2F;usr&#x2F;lib&#x2F;postgresql&#x2F;9.5&#x2F;bin&#x2F;repmgr standby follow -f &#x2F;etc&#x2F;postgresql&#x2F;repmgr.conf --log-to-file --upstream-node-id&#x3D;%n&#39;</span><br><span class="line">#上面这两个命令必须绝对路径</span><br></pre></td></tr></table></figure></li>
<li>主备均要启动 repmgrd 服务<br>配置如果改了，则需要 kill 掉，然后重新启动.<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">repmgrd -f &#x2F;etc&#x2F;postgresql&#x2F;repmgr.conf</span><br></pre></td></tr></table></figure></li>
<li>测试</li>
<li><ol>
<li>主库挂掉<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pg_ctl -D . stop</span><br></pre></td></tr></table></figure></li>
</ol>
</li>
<li>2 副库则看到<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">[2020-02-13 20:14:49] [INFO] checking state of node 1, 6 of 6 attempts</span><br><span class="line">[2020-02-13 20:14:49] [WARNING] unable to ping &quot;user&#x3D;postgres password&#x3D;d connect_timeout&#x3D;2 dbname&#x3D;postgres host&#x3D;db1 fallback_application_name&#x3D;repmgr&quot;</span><br><span class="line">[2020-02-13 20:14:49] [DETAIL] PQping() returned &quot;PQPING_NO_RESPONSE&quot;</span><br><span class="line">[2020-02-13 20:14:49] [WARNING] unable to reconnect to node 1 after 6 attempts</span><br><span class="line">[2020-02-13 20:14:49] [INFO] 0 active sibling nodes registered</span><br><span class="line">[2020-02-13 20:14:49] [INFO] primary and this node have the same location (&quot;default&quot;)</span><br><span class="line">[2020-02-13 20:14:49] [INFO] no other sibling nodes - we win by default</span><br><span class="line">[2020-02-13 20:14:49] [NOTICE] this node is the only available candidate and will now promote itself</span><br><span class="line">[2020-02-13 20:14:49] [INFO] promote_command is:</span><br><span class="line">&quot;&#x2F;usr&#x2F;lib&#x2F;postgresql&#x2F;9.5&#x2F;bin&#x2F;repmgr standby promote -f &#x2F;etc&#x2F;postgresql&#x2F;repmgr.conf --log-to-file&quot;</span><br><span class="line">[2020-02-13 20:14:49] [NOTICE] promoting standby to primary</span><br><span class="line">[2020-02-13 20:14:49] [DETAIL] promoting server &quot;139&quot; (ID: 2) using &quot;&#x2F;usr&#x2F;lib&#x2F;postgresql&#x2F;9.5&#x2F;bin&#x2F;pg_ctl  -w -D &#39;&#x2F;var&#x2F;lib&#x2F;postgresql&#x2F;9.5&#x2F;main&#39; promote&quot;</span><br><span class="line">LOG:  received promote request</span><br><span class="line">[2020-02-13 20:14:49] [NOTICE] LOG:  redo done at 0&#x2F;5000028</span><br><span class="line">waiting up to 60 seconds (parameter &quot;promote_check_timeout&quot;) for promotion to complete</span><br><span class="line">LOG:  last completed transaction was at log time 2020-02-13 04:13:29.417036-08</span><br><span class="line">LOG:  selected new timeline ID: 2</span><br><span class="line">LOG:  archive recovery complete</span><br><span class="line">LOG:  MultiXact member wraparound protections are now enabled</span><br><span class="line">LOG:  database system is ready to accept connections</span><br><span class="line">LOG:  autovacuum launcher started</span><br><span class="line">[2020-02-13 20:14:50] [NOTICE] STANDBY PROMOTE successful</span><br><span class="line">[2020-02-13 20:14:50] [DETAIL] server &quot;139&quot; (ID: 2) was successfully promoted to primary</span><br><span class="line">[2020-02-13 20:14:50] [INFO] 0 followers to notify</span><br><span class="line">[2020-02-13 20:14:50] [INFO] switching to primary monitoring mode</span><br><span class="line">[2020-02-13 20:14:50] [NOTICE] monitoring cluster primary &quot;139&quot; (ID: 2)</span><br></pre></td></tr></table></figure>
可以看到副库已经变成了主库<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">postgres@ubuntu:~&#x2F;9.5&#x2F;main$ repmgr cluster show -f &#x2F;etc&#x2F;postgresql&#x2F;repmgr.conf</span><br><span class="line"> ID | Name | Role    | Status    | Upstream | Location | Priority | Connection string</span><br><span class="line">----+------+---------+-----------+----------+----------+----------+---------------------------------------------------------------------</span><br><span class="line"> 1  | 138  | primary | - failed  |          | default  | 100      | host&#x3D;db1 user&#x3D;postgres dbname&#x3D;postgres password&#x3D;d connect_timeout&#x3D;2</span><br><span class="line"> 2  | 139  | primary | * running |          | default  | 100      | host&#x3D;db2 user&#x3D;postgres dbname&#x3D;postgres password&#x3D;d connect_timeout&#x3D;2</span><br><span class="line"></span><br><span class="line">WARNING: following issues were detected</span><br><span class="line">  - unable to connect to node &quot;138&quot; (ID: 1)</span><br></pre></td></tr></table></figure></li>
<li>3 恢复<br>也就是将主库设置为新的从库<br>创建 recovery.conf 文件。<br>内部配置和备用库基本一样<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">standby_mode &#x3D; &#39;on&#39; # 热备份mode</span><br><span class="line">primary_conninfo &#x3D; &#39;user&#x3D;repl password&#x3D;d host&#x3D;192.168.56.139 port&#x3D;5432 sslmode&#x3D;prefer sslcompression&#x3D;1 krbsrvname&#x3D;postgres&#39; #  连接信息</span><br><span class="line">recovery_target_timeline &#x3D; &#39;latest&#39; # 复制最新数据</span><br></pre></td></tr></table></figure>
启动<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pg_ctl start</span><br></pre></td></tr></table></figure>
可以看到<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">postgres@ubuntu:~&#x2F;9.5&#x2F;main$ repmgr cluster show -f &#x2F;etc&#x2F;postgresql&#x2F;repmgr.conf</span><br><span class="line"> ID | Name | Role    | Status    | Upstream | Location | Priority | Connection string</span><br><span class="line">----+------+---------+-----------+----------+----------+----------+--------------------------------------------------------------------                      -</span><br><span class="line"> 1  | 138  | standby |   running |          | default  | 100      | host&#x3D;db1 user&#x3D;postgres dbname&#x3D;postgres password&#x3D;d connect_timeout&#x3D;2</span><br><span class="line"> 2  | 139  | primary | * running |          | default  | 100      | host&#x3D;db2 user&#x3D;postgres dbname&#x3D;postgres password&#x3D;d connect_timeout&#x3D;2</span><br></pre></td></tr></table></figure>
成功切换</li>
</ol>
<h2 id="问题"><a href="#问题" class="headerlink" title="问题"></a>问题</h2><ol>
<li><p>apt 安装后并配置数据库会导致启动数据库时could not access file “repmgr”: No such file or directory<br>It means the repmgr extension code is not installed in the PostgreSQL application directory. This typically happens when using PostgreSQL packages provided by a third-party vendor, which often have different filesystem layouts.</p>
<p>Either use PostgreSQL packages provided by the community or 2ndQuadrant; if this is not possible, contact your vendor for assistance.<br>简单就是说用的可能时第三方的包，还是从官网下载包并编译安装吧。</p>
</li>
<li><p>提示未输入密码</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">ERROR: connection to database failed</span><br><span class="line">DETAIL:</span><br><span class="line">fe_sendauth: no password supplied</span><br><span class="line"></span><br><span class="line">ERROR: unable to establish necessary replication connections</span><br><span class="line">HINT: check replication permissions on the source server</span><br></pre></td></tr></table></figure>
<p>检查 pg_hba.conf，以及 ~/.pgpass 是否输入正确的用户密码。没找到问题，然后重新初始化数据库部署了一次，就好了。</p>
<h2 id="常用命令"><a href="#常用命令" class="headerlink" title="常用命令"></a>常用命令</h2><h2 id="参考"><a href="#参考" class="headerlink" title="参考:"></a>参考:</h2></li>
<li><p><a href="https://repmgr.org/docs/repmgr.html" target="_blank" rel="noopener">https://repmgr.org/docs/repmgr.html</a></p>
</li>
<li><p><a href="http://www.cslingjun.com/2019/11/27/PostgreSQL%E9%AB%98%E5%8F%AF%E7%94%A8%E9%9B%86%E7%BE%A4repmgr/" target="_blank" rel="noopener">http://www.cslingjun.com/2019/11/27/PostgreSQL%E9%AB%98%E5%8F%AF%E7%94%A8%E9%9B%86%E7%BE%A4repmgr/</a></p>
</li>
</ol>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://ddota2.gitee.io/2020/02/11/pg-pool/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/h_something/images/avatar.gif">
      <meta itemprop="name" content="Dota2 player">
      <meta itemprop="description" content="楼下的男人病的要死">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="D">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/h_something/2020/02/11/pg-pool/" class="post-title-link" itemprop="url">pg_pool 简单教程</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-02-11 10:35:48" itemprop="dateCreated datePublished" datetime="2020-02-11T10:35:48+08:00">2020-02-11</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-11-19 16:51:59" itemprop="dateModified" datetime="2020-11-19T16:51:59+08:00">2020-11-19</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/h_something/categories/postgresql/" itemprop="url" rel="index"><span itemprop="name">postgresql</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <h1 id="pg-pool使用"><a href="#pg-pool使用" class="headerlink" title="pg_pool使用"></a>pg_pool使用</h1><h2 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h2><p>Pgpool-II 是一个位于 PostgreSQL服务器和 PostgreSQL数据库客户端之间的中间件，Pgpool-II提供了连接池（Connection Pooling）、复制（Replication）、负载均衡（Load Balancing）、缓存（In Memory Query Cache）、看门狗（Watchdog）、超出限制链接（Limiting Exceeding Connections）等功能，可以基于这些特性来搭建PostgreSQL高可用集群。</p>
<h2 id="pg-pool安装"><a href="#pg-pool安装" class="headerlink" title="pg_pool安装"></a>pg_pool安装</h2><ol>
<li><p>首先前往<a href="https://www.pgpool.net/mediawiki/index.php/Downloads，下载对应的源码包,并解压。" target="_blank" rel="noopener">https://www.pgpool.net/mediawiki/index.php/Downloads，下载对应的源码包,并解压。</a></p>
</li>
<li><p>进入解压的根目录,安装。</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">apt install postgresql-server-dev-9.5 # 安装插件前需要安装 pg-dev</span><br><span class="line">.&#x2F;configure --prefix&#x3D;&#x2F;opt&#x2F;pgpool</span><br><span class="line">make</span><br><span class="line">make install</span><br></pre></td></tr></table></figure>
<p> –prefix 为安装后的目录前缀，也就是根目录<br> 若出现<br> configure 错误<br> checking for  PQexecPrepared  in  -lpq…  no<br> configure: error: libpq is  not installed or libpq is old<br> 是由于找不到 pg lib 库,可添加参数-with-pgsql=pgsql lib路径。<br> 安装后如果找不到命令,要么进入 /opt/pgpool/bin 执行。<br> 推荐引入环境变量, /etc/profile  </p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">export PGPOOL&#x3D;&#x2F;opt&#x2F;pgpool</span><br><span class="line">export PATH&#x3D;$PATH:$PGPOOL&#x2F;bin</span><br></pre></td></tr></table></figure>
<p> 最好将目录所有者也修改</p>
 <figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">chown -R postgres.postgres /opt/pgpool</span><br></pre></td></tr></table></figure></li>
<li><p>进入解压后的目录，安装相关函数。<br>比如:/home/d/pgpool-II-3.7.12/src/sql。<br>分别进入pgpool_adm，pgpool-recovery，pgpool-regclass文件夹，进行 make &amp; make install。<br>检查: 进入/usr/share/postgresql/9.5/extension目录，查看是否有相关函数文件。</p>
</li>
<li><p>配置<br> 根据安装路径不同，配置文件的路径也不一样，比如例子中配置文件路径在/opt/pgpool/etc。<br> 有时候也可能不再安装路径，而在/usr/local/etc。要区分到底使用的是哪一个路径的配置。<br> 可通过直接执行 pgpool 根据获取安装路径。<br> 复制一份 sample 用于修改。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">cp pcp.conf.sample pcp.conf</span><br><span class="line">cp pgpool.conf.sample pgpool.conf</span><br><span class="line">cp pool_hba.conf.sample pool_hba.conf</span><br></pre></td></tr></table></figure>
<p>4.1 pcp.conf<br>执行 pg_md5 用户的密码,比如 </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">postgres@ubuntu:/opt/pgpool/etc$ pg_md5  d</span><br><span class="line">8277e0910d750195b448797616e091ad</span><br></pre></td></tr></table></figure>
<p>编辑 pcp.conf, 在末尾输入 USERID:MD5PASSWD。<br>eg:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">postgres:8277e0910d750195b448797616e091ad</span><br></pre></td></tr></table></figure>
<p>4.2 pool_hba.conf<br>任何地址连接任何用户的全部使用 md5 验证</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">local   all         all                               md5</span><br><span class="line">host    all         all         0.0.0.0&#x2F;0          md5</span><br></pre></td></tr></table></figure>
<p>4.3 pgpool.conf<br>仅列出简要部分</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br></pre></td><td class="code"><pre><span class="line"># - pgpool Connection Settings -</span><br><span class="line">listen_addresses &#x3D; &#39;*&#39;</span><br><span class="line">port &#x3D; 9999</span><br><span class="line"></span><br><span class="line"># - Backend Connection Settings -</span><br><span class="line"></span><br><span class="line">backend_hostname0 &#x3D; &#39;db1&#39;</span><br><span class="line">backend_port0 &#x3D; 5432</span><br><span class="line">backend_weight0 &#x3D; 1</span><br><span class="line">backend_data_directory0 &#x3D; &#39;&#x2F;var&#x2F;lib&#x2F;postgresql&#x2F;9.5&#x2F;main&#39;</span><br><span class="line">backend_flag0 &#x3D; &#39;ALLOW_TO_FAILOVER&#39;</span><br><span class="line"></span><br><span class="line">backend_hostname1 &#x3D; &#39;db2&#39;</span><br><span class="line">backend_port1 &#x3D; 5433</span><br><span class="line">backend_weight1 &#x3D; 1</span><br><span class="line">backend_data_directory1 &#x3D; &#39;&#x2F;var&#x2F;lib&#x2F;postgresql&#x2F;9.5&#x2F;main&#39;</span><br><span class="line">backend_flag1 &#x3D; &#39;ALLOW_TO_FAILOVER&#39;    </span><br><span class="line"></span><br><span class="line"># - Authentication -         </span><br><span class="line">enable_pool_hba &#x3D; on     </span><br><span class="line">pool_passwd &#x3D; &#39;pool_passwd&#39;</span><br><span class="line">authentication_timeout &#x3D; 60</span><br><span class="line"></span><br><span class="line">#------------------------------------------------------------------------------</span><br><span class="line"># FILE LOCATIONS</span><br><span class="line">#------------------------------------------------------------------------------</span><br><span class="line"></span><br><span class="line">pid_file_name &#x3D; &#39;&#x2F;opt&#x2F;pgpool&#x2F;pgpool.pid&#39;</span><br><span class="line">logdir &#x3D; &#39;&#x2F;var&#x2F;log&#x2F;pgpool&#39;</span><br><span class="line"># 以上这两个目录均要设置所有者为执行命令的用户,此例子中是 postgres</span><br><span class="line"></span><br><span class="line">#------------------------------------------------------------------------------</span><br><span class="line"># LOAD BALANCING MODE</span><br><span class="line">#------------------------------------------------------------------------------</span><br><span class="line"></span><br><span class="line">load_balance_mode &#x3D; on   </span><br><span class="line"></span><br><span class="line">#------------------------------------------------------------------------------</span><br><span class="line"># MASTER&#x2F;SLAVE MODE</span><br><span class="line">#------------------------------------------------------------------------------</span><br><span class="line"></span><br><span class="line">master_slave_mode &#x3D; on</span><br><span class="line">master_slave_sub_mode &#x3D; &#39;stream&#39;</span><br><span class="line"></span><br><span class="line"># - Streaming -</span><br><span class="line">sr_check_period &#x3D; 5</span><br><span class="line">sr_check_user &#x3D; &#39;postgres&#39;</span><br><span class="line">sr_check_password &#x3D; &#39;d&#39;</span><br><span class="line">sr_check_database &#x3D; &#39;postgres&#39;</span><br><span class="line">delay_threshold &#x3D; 0</span><br><span class="line"></span><br><span class="line">#------------------------------------------------------------------------------</span><br><span class="line"># HEALTH CHECK GLOBAL PARAMETERS</span><br><span class="line">#------------------------------------------------------------------------------</span><br><span class="line"></span><br><span class="line">health_check_period &#x3D; 10</span><br><span class="line">health_check_timeout &#x3D; 20</span><br><span class="line">health_check_user &#x3D; &#39;postgres&#39;</span><br><span class="line">health_check_password &#x3D; &#39;d&#39;</span><br><span class="line">health_check_database &#x3D; &#39;postgres&#39;</span><br><span class="line">health_check_max_retries &#x3D; 0</span><br><span class="line">health_check_retry_delay &#x3D; 1</span><br><span class="line">connect_timeout &#x3D; 10000</span><br><span class="line"></span><br><span class="line">#------------------------------------------------------------------------------</span><br><span class="line"># FAILOVER AND FAILBACK</span><br><span class="line">#------------------------------------------------------------------------------</span><br><span class="line">failover_command &#x3D; &#39;&#x2F;opt&#x2F;pgpool&#x2F;failover_stream.sh %H %R&#39;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">#------------------------------------------------------------------------------</span><br><span class="line"># WATCHDOG 看门狗</span><br><span class="line">#------------------------------------------------------------------------------</span><br><span class="line"></span><br><span class="line"># - Enabling -</span><br><span class="line"></span><br><span class="line">use_watchdog &#x3D; on</span><br><span class="line">                                    # Activates watchdog</span><br><span class="line">                                    # (change requires restart)</span><br><span class="line"></span><br><span class="line"># -Connection to up stream servers -</span><br><span class="line"></span><br><span class="line">trusted_servers &#x3D; &#39;&#39;</span><br><span class="line">                                    # trusted server list which are used</span><br><span class="line">                                    # to confirm network connection</span><br><span class="line">                                    # (hostA,hostB,hostC,...)</span><br><span class="line">                                    # (change requires restart)</span><br><span class="line">ping_path &#x3D; &#39;&#x2F;bin&#39;</span><br><span class="line">                                    # ping command path</span><br><span class="line">                                    # (change requires restart)</span><br><span class="line"></span><br><span class="line"># - Watchdog communication Settings -</span><br><span class="line"></span><br><span class="line">wd_hostname &#x3D; &#39;db1&#39; # 此看门狗IP，即本机</span><br><span class="line">wd_port &#x3D; 9000</span><br><span class="line">wd_priority &#x3D; 1</span><br><span class="line">wd_authkey &#x3D; &#39;&#39;</span><br><span class="line">wd_ipc_socket_dir &#x3D; &#39;&#x2F;tmp&#39;</span><br><span class="line"></span><br><span class="line"># - Virtual IP control Setting -</span><br><span class="line"></span><br><span class="line">delegate_IP &#x3D; &#39;192.168.56.122&#39; # 浮动IP</span><br><span class="line">if_cmd_path &#x3D; &#39;&#x2F;sbin&#39; # if命令执行的路径</span><br><span class="line">if_up_cmd &#x3D; &#39;ip addr add $_IP_$&#x2F;24 dev ens33 label ens33:0&#39;</span><br><span class="line">if_down_cmd &#x3D; &#39;ip addr del $_IP_$&#x2F;24 dev ens33&#39;</span><br><span class="line">                                    # shutdown delegate IP command</span><br><span class="line">                                    # (change requires restart)</span><br><span class="line">arping_path &#x3D; &#39;&#x2F;usr&#x2F;sbin&#39;</span><br><span class="line">                                    # arping command path</span><br><span class="line">                                    # (change requires restart)</span><br><span class="line">arping_cmd &#x3D; &#39;arping -U $_IP_$ -w 1 -I ens33&#39;</span><br><span class="line">                                    # arping command</span><br><span class="line">                                    # (change requires restart)</span><br><span class="line"># 需要给命令 chmod u+s ,以赋予执行时的 ROOT 权限。   </span><br><span class="line"></span><br><span class="line"># -- heartbeat mode --</span><br><span class="line"></span><br><span class="line">wd_heartbeat_port &#x3D; 9694</span><br><span class="line">wd_heartbeat_keepalive &#x3D; 2</span><br><span class="line">wd_heartbeat_deadtime &#x3D; 30</span><br><span class="line"></span><br><span class="line"># 可填写多个终端</span><br><span class="line">heartbeat_destination0 &#x3D; &#39;db2&#39; # 终端0 IP</span><br><span class="line">heartbeat_destination_port0 &#x3D; 9694 # 终端0 端口</span><br><span class="line">heartbeat_device0 &#x3D; &#39;ens33&#39;         </span><br><span class="line"></span><br><span class="line"># -- query mode --</span><br><span class="line"></span><br><span class="line">wd_life_point &#x3D; 3</span><br><span class="line">wd_lifecheck_query &#x3D; &#39;SELECT 1&#39;</span><br><span class="line">wd_lifecheck_dbname &#x3D; &#39;postgres&#39;</span><br><span class="line">wd_lifecheck_user &#x3D; &#39;postgres&#39;</span><br><span class="line">wd_lifecheck_password &#x3D; &#39;d&#39;      </span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># - Other pgpool Connection Settings -</span><br><span class="line"># 其他 pool 连接</span><br><span class="line"></span><br><span class="line">other_pgpool_hostname0 &#x3D; &#39;db2&#39;</span><br><span class="line">other_pgpool_port0 &#x3D; 5432</span><br><span class="line">other_wd_port0 &#x3D; 9000</span><br></pre></td></tr></table></figure>
<p>配置 pool_passwd</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pg_md5 -p -m -u postgres pool_passwd</span><br></pre></td></tr></table></figure>
<p>日志目录创建以及所有者更改</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">root@ubuntu:/var/<span class="built_in">log</span><span class="comment"># mkdir pgpool</span></span><br><span class="line">root@ubuntu:/var/<span class="built_in">log</span><span class="comment"># chown postgres.postgres pgpool</span></span><br></pre></td></tr></table></figure>
<p>创建 故障转移脚本<br>vim /opt/pgpool/failover_stream.sh</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">#! &#x2F;bin&#x2F;sh </span><br><span class="line"># Failover command for streaming replication. </span><br><span class="line"># Arguments: $1: new master hostname. </span><br><span class="line"></span><br><span class="line">new_master&#x3D;$1 </span><br><span class="line">new_data&#x3D;$2</span><br><span class="line">trigger_command&#x3D;&quot;$PGHOME&#x2F;bin&#x2F;pg_ctl promote -D $2&quot; </span><br><span class="line"></span><br><span class="line"># Prompte standby database. </span><br><span class="line">&#x2F;usr&#x2F;bin&#x2F;ssh -T $new_master $trigger_command </span><br><span class="line"></span><br><span class="line">exit 0;</span><br></pre></td></tr></table></figure>
<p>配置 ssh 密钥</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">ssh-keygen -t rsa</span><br><span class="line">cat ~&#x2F;.ssh&#x2F;id_rsa.pub &gt;&gt; ~&#x2F;.ssh&#x2F;authorized_keys</span><br><span class="line">chmod 600 ~&#x2F;.ssh&#x2F;authorized_keys</span><br></pre></td></tr></table></figure>
<p>最好都手动连接一下,免得要你写 yes。<br>修改 hosts<br>vim /etc/hosts<br>后面添加</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">192.168.56.139 db1</span><br><span class="line">192.168.56.138 db2</span><br><span class="line">192.168.56.177 vip</span><br></pre></td></tr></table></figure>
<p>ip 相关命令赋予权限</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">chmod u+s &#x2F;sbin&#x2F;ip</span><br><span class="line">apt install arping</span><br><span class="line">chmod u+s &#x2F;usr&#x2F;sbin&#x2F;arping</span><br></pre></td></tr></table></figure>
<p>不同服务器上配置文件大致相同, 主要是 pgpool.conf 修改一下部分:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">wd_hostname &#x3D; &#39;&#39;  修改为本机IP</span><br><span class="line">heartbeat_destination0 &#x3D; &#39;&#39; # 修改为其他 pool 服务器IP</span><br><span class="line">other_pgpool_hostname0 &#x3D; &#39;&#39; # 修改为其他pool 服务器IP</span><br></pre></td></tr></table></figure>
<h2 id="启动"><a href="#启动" class="headerlink" title="启动"></a>启动</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pgpool -n -D</span><br></pre></td></tr></table></figure>
<p>启动后，可以看到多了一个IP，也就是浮动IP。也就代表成功了</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">postgres@ubuntu:~&#x2F;9.5&#x2F;main$ ip addr</span><br><span class="line">1: lo: &lt;LOOPBACK,UP,LOWER_UP&gt; mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1</span><br><span class="line">    link&#x2F;loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00</span><br><span class="line">    inet 127.0.0.1&#x2F;8 scope host lo</span><br><span class="line">    valid_lft forever preferred_lft forever</span><br><span class="line">    inet6 ::1&#x2F;128 scope host</span><br><span class="line">    valid_lft forever preferred_lft forever</span><br><span class="line">2: ens33: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc pfifo_fast state UP group default qlen 1000</span><br><span class="line">    link&#x2F;ether 00:0c:29:ca:40:8f brd ff:ff:ff:ff:ff:ff</span><br><span class="line">    inet 192.168.56.139&#x2F;24 brd 192.168.56.255 scope global ens33</span><br><span class="line">    valid_lft forever preferred_lft forever</span><br><span class="line">    inet 192.168.56.122&#x2F;24 scope global secondary ens33:0</span><br><span class="line">    valid_lft forever preferred_lft forever</span><br><span class="line">    inet6 fe80::20c:29ff:feca:408f&#x2F;64 scope link</span><br><span class="line">    valid_lft forever preferred_lft forever</span><br></pre></td></tr></table></figure>

</li>
</ol>
<h2 id="测试"><a href="#测试" class="headerlink" title="测试"></a>测试</h2><h3 id="测试数据库挂掉"><a href="#测试数据库挂掉" class="headerlink" title="测试数据库挂掉"></a>测试数据库挂掉</h3><p>启动<br>主</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">postgres@ubuntu:~$ pgpool -n -D</span><br><span class="line">postgres@ubuntu:~&#x2F;9.5&#x2F;main$ pgpool -D . start</span><br><span class="line"></span><br><span class="line">(2 rows)</span><br></pre></td></tr></table></figure>
<p>副</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">postgres@ubuntu:~&#x2F;9.5&#x2F;main$ pgpool -D . start</span><br></pre></td></tr></table></figure>
<p>连接数据库</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">postgres@ubuntu:~&#x2F;9.5&#x2F;main$ psql -U postgres -h vip -p 9999</span><br><span class="line">postgres&#x3D;# show pool_nodes;</span><br><span class="line">node_id | hostname | port | status | lb_weight |  role   | select_cnt | load_balance_node | replication_delay</span><br><span class="line">---------+----------+------+--------+-----------+---------+------------+-------------------+-------------------</span><br><span class="line">0       | db1      | 5432 | up     | 0.500000  | primary | 0          | true              | 0</span><br><span class="line">1       | db2      | 5432 | up     | 0.500000  | standby | 0          | false             | 0</span><br></pre></td></tr></table></figure>
<p>关闭主数据库</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">postgres@ubuntu:~&#x2F;9.5&#x2F;main$ pg_ctl -D . stop</span><br></pre></td></tr></table></figure>
<p>检查结果</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">postgres@ubuntu:~&#x2F;9.5&#x2F;main$ psql -U postgres -h vip -p 9999</span><br><span class="line">postgres&#x3D;# show pool_nodes;</span><br><span class="line">node_id | hostname | port | status | lb_weight |  role   | select_cnt | load_balance_node | replication_delay</span><br><span class="line">---------+----------+------+--------+-----------+---------+------------+-------------------+-------------------</span><br><span class="line">0       | db1      | 5432 | down   | 0.500000  | standby | 0          | false             | 0</span><br><span class="line">1       | db2      | 5432 | up     | 0.500000  | primary | 0          | true              | 0</span><br><span class="line">(2 rows)</span><br></pre></td></tr></table></figure>
<p>以上结果显示，db1（旧主）已经 down, 且 role 由 primary 变成 standby。db2 role 由 standby 变成 primary。也就是切换成功了。</p>
<h4 id="恢复-以下操作均在-旧master-上面执行"><a href="#恢复-以下操作均在-旧master-上面执行" class="headerlink" title="恢复(以下操作均在 旧master 上面执行)"></a>恢复(以下操作均在 旧master 上面执行)</h4><p>先倒带</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pg_rewind --target-pgdata&#x3D;&#x2F;var&#x2F;lib&#x2F;postgresql&#x2F;9.5&#x2F;main --source-server&#x3D;&#39;host&#x3D;192.168.56.138 port&#x3D;5432 user&#x3D;postgres password&#x3D;d dbname&#x3D;postgres&#39; -P</span><br></pre></td></tr></table></figure>
<p>重命名recovery.done =&gt; recovery.conf，并配置。值得注意的是 host 要填写新的 master 的IP。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">postgres@ubuntu:~&#x2F;9.5&#x2F;main$ mv recovery.done recovery.conf</span><br><span class="line">postgres@ubuntu:~&#x2F;9.5&#x2F;main$ cat recovery.conf</span><br><span class="line">standby_mode &#x3D; &#39;on&#39;</span><br><span class="line">primary_conninfo &#x3D; &#39;user&#x3D;repl password&#x3D;d host&#x3D;192.168.56.138 port&#x3D;5432 sslmode&#x3D;prefer sslcompression&#x3D;1 krbsrvname&#x3D;postgres&#39;</span><br><span class="line">recovery_target_timeline &#x3D; &#39;latest&#39;</span><br></pre></td></tr></table></figure>
<p>启动</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">postgres@ubuntu:~&#x2F;9.5&#x2F;main$ pg_ctl -D . start</span><br></pre></td></tr></table></figure>
<p>附加到pgpool</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pcp_attach_node -d -U postgres -h vip -p 9898 -n 0</span><br></pre></td></tr></table></figure>
<p>查看结果</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">postgres@ubuntu:~&#x2F;9.5&#x2F;main$ psql -U postgres -h vip -p 9999</span><br><span class="line">postgres&#x3D;# show pool_nodes;</span><br><span class="line">node_id | hostname | port | status | lb_weight |  role   | select_cnt | load_balance_node | replication_delay</span><br><span class="line">---------+----------+------+--------+-----------+---------+------------+-------------------+-------------------</span><br><span class="line">0       | db1      | 5432 | up     | 0.500000  | standby | 0          | false             | 0</span><br><span class="line">1       | db2      | 5432 | up     | 0.500000  | primary | 0          | true              | 0</span><br><span class="line">(2 rows)</span><br></pre></td></tr></table></figure>
<p>以上结果显示 db1 已经启动。<br>在新master 上执行</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">postgres@ubuntu:~$ psql</span><br><span class="line">select * from pg_stat_replication;</span><br><span class="line">pid  | usesysid | usename | application_name |  client_addr   | client_hostname | client_port |         backend_start         | backend_xmin |   state   | sent_location | write_location | flush_location | replay_location | sync_priority | sync_state</span><br><span class="line">-------+----------+---------+------------------+----------------+-----------------+-------------+-------------------------------+--------------+-----------+---------------+----------------+----------------+-----------------+---------------+------------</span><br><span class="line"> 47886 |    16384 | repl    | walreceiver      | 192.168.56.139 |                 |       53320 | 2020-02-09 20:27:17.640766-08 |              | streaming | 0&#x2F;1A000368    | 0&#x2F;1A000368     | 0&#x2F;1A000368     | 0&#x2F;1A000368      |             0 | async</span><br><span class="line">(1 row)</span><br></pre></td></tr></table></figure>
<p>显示新的备份服务器已经关联到新的master服务器。<br>完美！</p>
<h3 id="测试-pgpool-挂掉"><a href="#测试-pgpool-挂掉" class="headerlink" title="测试 pgpool 挂掉"></a>测试 pgpool 挂掉</h3><p>主启动,副启动, 除了pgpool.conf 有些许不同，其他基本一样。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">postgres@ubuntu:~$ pgpool -n -D</span><br><span class="line">postgres@ubuntu:~&#x2F;9.5&#x2F;main$ pgpool -D . start</span><br></pre></td></tr></table></figure>
<p>停止主 pgpool, 还是不知道为什么停止很久还是无法停止。<br>我的临时解决方式是以 pgpool -n 方式启动, 然后 ctrl+c 将它停止。<br>直接 kill 会导致很多问题，有很多其他的任务无法被关闭。只能重启。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pgpool -m fast stop</span><br></pre></td></tr></table></figure>
<p>master server ip</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">postgres@ubuntu:~&#x2F;9.5&#x2F;main$ ip addr</span><br><span class="line">1: lo: &lt;LOOPBACK,UP,LOWER_UP&gt; mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1</span><br><span class="line">    link&#x2F;loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00</span><br><span class="line">    inet 127.0.0.1&#x2F;8 scope host lo</span><br><span class="line">       valid_lft forever preferred_lft forever</span><br><span class="line">    inet6 ::1&#x2F;128 scope host</span><br><span class="line">       valid_lft forever preferred_lft forever</span><br><span class="line">2: ens33: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc pfifo_fast state UP group default qlen 1000</span><br><span class="line">    link&#x2F;ether 00:0c:29:ca:40:8f brd ff:ff:ff:ff:ff:ff</span><br><span class="line">    inet 192.168.56.139&#x2F;24 brd 192.168.56.255 scope global ens33</span><br><span class="line">       valid_lft forever preferred_lft forever</span><br><span class="line">    inet6 fe80::20c:29ff:feca:408f&#x2F;64 scope link</span><br><span class="line">       valid_lft forever preferred_lft forever</span><br></pre></td></tr></table></figure>
<p>副服务器IP</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">root@ubuntu:&#x2F;var&#x2F;log# ip addr</span><br><span class="line">1: lo: &lt;LOOPBACK,UP,LOWER_UP&gt; mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1</span><br><span class="line">    link&#x2F;loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00</span><br><span class="line">    inet 127.0.0.1&#x2F;8 scope host lo</span><br><span class="line">       valid_lft forever preferred_lft forever</span><br><span class="line">    inet6 ::1&#x2F;128 scope host</span><br><span class="line">       valid_lft forever preferred_lft forever</span><br><span class="line">2: ens33: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc pfifo_fast state UP group default qlen 1000</span><br><span class="line">    link&#x2F;ether 00:0c:29:0c:df:8d brd ff:ff:ff:ff:ff:ff</span><br><span class="line">    inet 192.168.56.138&#x2F;24 brd 192.168.56.255 scope global ens33</span><br><span class="line">       valid_lft forever preferred_lft forever</span><br><span class="line">    inet 192.168.56.122&#x2F;24 scope global secondary ens33:0</span><br><span class="line">       valid_lft forever preferred_lft forever</span><br><span class="line">    inet6 fe80::20c:29ff:fe0c:df8d&#x2F;64 scope link</span><br><span class="line">       valid_lft forever preferred_lft forever</span><br></pre></td></tr></table></figure>
<p>可以看到服务器 IP 已经浮动到副服务器上。</p>
<h4 id="恢复"><a href="#恢复" class="headerlink" title="恢复"></a>恢复</h4><p>恢复很简单，只要重新启动 pgpool 即可。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pgpool -n -D</span><br></pre></td></tr></table></figure>
<p>如下结果，即代表连接成功，否则状态可能为 SHUTDOWN。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">postgres@ubuntu:&#x2F;opt&#x2F;pgpool&#x2F;bin$ pcp_watchdog_info</span><br><span class="line">Password:</span><br><span class="line">2 YES db1:9999 Linux ubuntu db1</span><br><span class="line"></span><br><span class="line">db1:9999 Linux ubuntu db1 9999 9000 4 MASTER</span><br><span class="line">db2:9999 Linux ubuntu db2 9999 9000 7 STANDBY</span><br></pre></td></tr></table></figure>
<h3 id="测试-pgpool-以及-postgresql-数据库"><a href="#测试-pgpool-以及-postgresql-数据库" class="headerlink" title="测试 pgpool 以及 postgresql 数据库"></a>测试 pgpool 以及 postgresql 数据库</h3><p>简单粗暴,直接 shutdown master 服务器。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">shutdown now</span><br></pre></td></tr></table></figure>
<p>检查备用服务器IP</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">postgres@ubuntu:~&#x2F;9.5&#x2F;main$  ip addr</span><br><span class="line">1: lo: &lt;LOOPBACK,UP,LOWER_UP&gt; mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1</span><br><span class="line">    link&#x2F;loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00</span><br><span class="line">    inet 127.0.0.1&#x2F;8 scope host lo</span><br><span class="line">       valid_lft forever preferred_lft forever</span><br><span class="line">    inet6 ::1&#x2F;128 scope host</span><br><span class="line">       valid_lft forever preferred_lft forever</span><br><span class="line">2: ens33: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc pfifo_fast state UP group default qlen 1000</span><br><span class="line">    link&#x2F;ether 00:0c:29:0c:df:8d brd ff:ff:ff:ff:ff:ff</span><br><span class="line">    inet 192.168.56.138&#x2F;24 brd 192.168.56.255 scope global ens33</span><br><span class="line">       valid_lft forever preferred_lft forever</span><br><span class="line">    inet 192.168.56.122&#x2F;24 scope global secondary ens33:0</span><br><span class="line">       valid_lft forever preferred_lft forever</span><br><span class="line">    inet6 fe80::20c:29ff:fe0c:df8d&#x2F;64 scope link</span><br><span class="line">       valid_lft forever preferred_lft forever</span><br></pre></td></tr></table></figure>
<p>数据库状态</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">postgres@ubuntu:~&#x2F;9.5&#x2F;main$ psql -U postgres -h vip -p 9999</span><br><span class="line">postgres&#x3D;# show pool_nodes;</span><br><span class="line">node_id | hostname | port | status | lb_weight |  role   | select_cnt | load_balance_node | replication_delay</span><br><span class="line">---------+----------+------+--------+-----------+---------+------------+-------------------+-------------------</span><br><span class="line">0       | db1      | 5432 | down   | 0.500000  | standby | 0          | false             | 0</span><br><span class="line">1       | db2      | 5432 | up     | 0.500000  | primary | 0          | true              | 0</span><br><span class="line">(2 rows)</span><br></pre></td></tr></table></figure>
<p>正常工作。</p>
<h4 id="恢复-1"><a href="#恢复-1" class="headerlink" title="恢复"></a>恢复</h4><p>开机, 其实就是以上两个恢复操作都来一遍。<br>恢复数据库，主要就是四个步骤:</p>
<ol>
<li>倒带(pg_rewind);</li>
<li>编辑 recovery.conf;</li>
<li>启动(pg_ctl);</li>
<li>附加到 pgpool (pcp_attach_node)。<br>恢复 pgpool 就更简单了，只有启动。(pgpool)<br>不再赘述。<br>检查 pgpool。<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">postgres@ubuntu:&#x2F;opt&#x2F;pgpool&#x2F;bin$ pcp_watchdog_info</span><br><span class="line">Password:</span><br><span class="line">2 YES db2:9999 Linux ubuntu db2</span><br><span class="line"></span><br><span class="line">db2:9999 Linux ubuntu db2 9999 9000 4 MASTER</span><br><span class="line">db1:9999 Linux ubuntu db1 9999 9000 7 STANDBY</span><br></pre></td></tr></table></figure>
检查数据库 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">postgres@ubuntu:&#x2F;opt&#x2F;pgpool&#x2F;bin$ psql -U postgres -h vip -p 9999</span><br><span class="line">Password for user postgres:</span><br><span class="line">psql (9.5.19)</span><br><span class="line">Type &quot;help&quot; for help.</span><br><span class="line"></span><br><span class="line">postgres&#x3D;# show pool_nodes;</span><br><span class="line">node_id | hostname | port | status | lb_weight |  role   | select_cnt | load_balance_node | replication_delay</span><br><span class="line">---------+----------+------+--------+-----------+---------+------------+-------------------+-------------------</span><br><span class="line">0       | db1      | 5432 | up     | 0.500000  | standby | 0          | false             | 0</span><br><span class="line">1       | db2      | 5432 | up     | 0.500000  | primary | 1          | true              | 0</span><br><span class="line">(2 rows)</span><br></pre></td></tr></table></figure>
成功恢复！   </li>
</ol>
<h2 id="附录"><a href="#附录" class="headerlink" title="附录:"></a>附录:</h2><ol>
<li><p>配置文件介绍<br>pgpool.conf:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br></pre></td><td class="code"><pre><span class="line">listen_addresses: 监听来自哪里的请求</span><br><span class="line">port: pgpool 的主程序端口</span><br><span class="line">backend_hostname0:后端数据库连接0的hostname</span><br><span class="line">backend_port0:后端数据库0连接的port</span><br><span class="line">backend_weight0:后端数据库0连接的权重</span><br><span class="line">backend_data_directory0:后端数据库0的data 目录</span><br><span class="line">backend_flag0:控制后端的行为,可ALLOW_TO_FAILOVER(允许故障转移)，DISALLOW_TO_FAILOVE(不允许故障转移)，ALWAYS_MASTER（永远为主）</span><br><span class="line">#0可以依次递增</span><br><span class="line"></span><br><span class="line">enable_pool_hba: 是否开启pool_hba,他会前往目录寻找pool_hba.conf 文件。类似pg的hba配置，用于限制用户连接以及权限</span><br><span class="line">log_destination：log放在哪,有syslog和stderr。</span><br><span class="line">还有记录什么的参数。</span><br><span class="line">pid_file_name：pid 文件名。喜爱那个对于pgpool.conf的路径或者绝对路径</span><br><span class="line">logdir: 记录pgpool状态的文件。</span><br><span class="line">master_slave_mode： off or on 开启或者关闭主从模式</span><br><span class="line">master_slave_sub_mode: 主从子模式,分别为stream,slony,logical 默认stream</span><br><span class="line"></span><br><span class="line">sr_check_period： 流复制检查周期,默认0禁用</span><br><span class="line">sr_check_user: 流复制检查所用用户，即使金庸也要填写。</span><br><span class="line">sr_check_password： 流复制检查用户的密码</span><br><span class="line">sr_check_database： 流复制检查的数据库</span><br><span class="line">delay_threshold：未将查询调度给备用节点之前的阈值，单位为字节。不太理解</span><br><span class="line"></span><br><span class="line"> 健康检查全局参数</span><br><span class="line">health_check_period： 健康检查周期，0禁用</span><br><span class="line">health_check_timeout: 健康检查超时时间。</span><br><span class="line">health_check_user： 健康检查用户</span><br><span class="line">health_check_password： 健康检查密码</span><br><span class="line">health_check_database： 健康检查数据库</span><br><span class="line">health_check_max_retries： 放弃前健康检查最大重试失败次数</span><br><span class="line">health_check_retry_delay： 失败后间隔多少秒重试</span><br><span class="line">connect_timeout: 放弃连接前的超时时间。0表示无超市时间。单位为ms。仅用健康检查，不用于连接backend。</span><br><span class="line"></span><br><span class="line">failover_command：故障转义命令。 部分参数如下:</span><br><span class="line">                                # Executes this command at failover</span><br><span class="line">                                # Special values:</span><br><span class="line">                                #   %d &#x3D; node id </span><br><span class="line">                                #   %h &#x3D; host name   </span><br><span class="line">                                #   %p &#x3D; port number</span><br><span class="line">                                #   %D &#x3D; database cluster path </span><br><span class="line">                                #   %m &#x3D; new master node id </span><br><span class="line">                                #   %H &#x3D; hostname of the new master node </span><br><span class="line">                                #   %M &#x3D; old master node id  </span><br><span class="line">                                #   %P &#x3D; old primary node id</span><br><span class="line">                                #   %r &#x3D; new master port number</span><br><span class="line">                                #   %R &#x3D; new master database cluster path</span><br><span class="line">                                #   %% &#x3D; &#39;%&#39; character</span><br><span class="line"></span><br><span class="line"> use_watchdog: 是否开启看门狗 </span><br><span class="line"> wd_hostname： 此看门狗的hostname</span><br><span class="line"> wd_port: 看门口的port        </span><br><span class="line"></span><br><span class="line"> delegate_IP: 虚拟IP    </span><br><span class="line"> if_cmd_path &#x3D; &#39;&#x2F;sbin&#39; if_up&#x2F;down_cmd的目录  </span><br><span class="line"> if_up_cmd &#x3D; &#39;ip addr add $_IP_$&#x2F;24 dev eth0 label eth0:0&#39; 启动delegate_IP的命令</span><br><span class="line"> if_down_cmd &#x3D; &#39;ip addr del $_IP_$&#x2F;24 dev eth0&#39; 关闭delegate_IP的命令</span><br><span class="line"> arping_path &#x3D; &#39;&#x2F;usr&#x2F;sbin&#39; arping 的目录</span><br><span class="line"> arping_cmd &#x3D; &#39;arping -U $_IP_$ -w 1 -I eth0&#39; arping 的命令</span><br><span class="line"> 需要 chmod u+s XXX,让它可以被其他角色执行。</span><br><span class="line"></span><br><span class="line"> 心跳模式</span><br><span class="line"> wd_heartbeat_port:接收心跳信号的端口</span><br><span class="line"> wd_heartbeat_keepalive： 间隔多久发送心跳信号，单位秒</span><br><span class="line"> wd_heartbeat_deadtime： 心跳信号停滞时间间隔</span><br><span class="line"> heartbeat_destination0: 目标0的 hostname或者ip，用于发送心跳信号</span><br><span class="line"> heartbeat_destination_port0: 目标0的端口。</span><br><span class="line"> heartbeat_device0： NIC设备名称,比如(eth0)，猜测是网卡名字。用于发送心跳信号，仅在非空时有效。且 pgpool 具有root权限。</span><br><span class="line"></span><br><span class="line"> 查询模式</span><br><span class="line"> wd_life_point: 生命检查重试次数</span><br><span class="line"> wd_lifecheck_query: 从看门狗到pgpool的生命检查查询</span><br><span class="line"> wd_lifecheck_dbname： 生命检查连接的数据库</span><br><span class="line"> wd_lifecheck_user: 监听pgpool生命检查用户</span><br><span class="line"> wd_lifecheck_password： 生命检查密码</span><br><span class="line"></span><br><span class="line"> 其他pgpool的连接设置</span><br><span class="line"> other_pgpool_hostname0: pool0 的 host</span><br><span class="line"> other_pgpool_port0: pool0 的 port</span><br><span class="line"> other_wd_port0: pool0 的 看门狗 port</span><br></pre></td></tr></table></figure>
<h2 id="异常"><a href="#异常" class="headerlink" title="异常"></a>异常</h2></li>
<li><p>MD5 authentication is not supported in replication and master-slave modes.<br>首先需要将 pgpool.conf 设置 enable_pool_hba = on。 然后参要一下表格。<br>简单的解决方法就是将所有 pool_hba.conf 以及 pg_hba.conf 的登陆认证均设置为MD5。</p>
</li>
</ol>
<table>
<thead>
<tr>
<th>pg_hba.conf</th>
<th>pool_hba.conf</th>
<th>pool_passwd</th>
<th>result</th>
</tr>
</thead>
<tbody><tr>
<td>md5</td>
<td>md5</td>
<td>yes</td>
<td>md5 auth</td>
</tr>
<tr>
<td>md5</td>
<td>md5</td>
<td>no</td>
<td>“MD5” authentication with pgpool failed for user “XX”</td>
</tr>
<tr>
<td>md5</td>
<td>trust</td>
<td>yes/no</td>
<td>MD5 authentication is unsupported in replication, master-slave and parallel mode</td>
</tr>
<tr>
<td>trust</td>
<td>md5</td>
<td>yes</td>
<td>no auth</td>
</tr>
<tr>
<td>trust</td>
<td>md5</td>
<td>no</td>
<td>“MD5” authentication with pgpool failed for user “XX”</td>
</tr>
<tr>
<td>trust</td>
<td>trust</td>
<td>yes/no</td>
<td>no auth</td>
</tr>
</tbody></table>
<ol start="2">
<li>即使postgresql 启动了，status 却仍然显示 down。<br>重现步骤: 配置好 backend1，运行pgpool，然后停止此 postgresql 数据库。<br>这时候即使重新启动了postgresql 数据库, pgpool 还是会一直设置 backend1 为 down。<br>倘若只有一个 backend，就会导致无法连接 pgpool。<br>找到一个设置状态的地方, 也就是logdir 配置的路径。找了好久好久，才发现可以在 logdir 配置的目录设置 backend 状态。<br>但是目前发现只能手动设置解决此问题，还未找到更好的方式。<br>找到了更好的方法：<ol>
<li>启动时添加 -D 参数,清除状态启动。</li>
<li>使用 pcp_attach_node 再次附加上去。</li>
</ol>
</li>
</ol>
<h2 id="疑问"><a href="#疑问" class="headerlink" title="疑问"></a>疑问</h2><ol>
<li>pgpool stop 停止很久但是无法关闭。<br>在具有浮点IP的机器上关闭 pgpool 很久还是没有关闭。发现有一个原因是因为连接没有关闭，但是有时候连接关闭了，还添加了 -m fast 参数还是无法关闭。还未找到原因。<br>经过配置发现 开启 watchdog 会导致此问题。use_watchdog = off 则不会<br>提示如下。<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">2020-02-10 01:25:38: pid 28280: LOG:  Watchdog is shutting down</span><br><span class="line">2020-02-10 01:25:38: pid 28280: LOG:  waiting for escalation process to exit before starting de-escalation</span><br><span class="line">2020-02-10 01:25:39: pid 28280: LOG:  waiting for escalation process to exit before starting de-escalation</span><br><span class="line">2020-02-10 01:25:40: pid 28280: LOG:  waiting for escalation process to exit before starting de-escalation</span><br><span class="line">2020-02-10 01:25:41: pid 28280: LOG:  waiting for escalation process to exit before starting de-escalation</span><br><span class="line">2020-02-10 01:25:42: pid 28280: LOG:  waiting for escalation process to exit before starting de-escalation</span><br><span class="line">2020-02-10 01:25:43: pid 28280: LOG:  escalation process does not exited in time</span><br><span class="line">2020-02-10 01:25:43: pid 28280: DETAIL:  starting the de-escalation anyway</span><br><span class="line">2020-02-10 01:25:43: pid 40639: LOG:  watchdog: de-escalation started</span><br><span class="line">2020-02-10 01:25:43: pid 40639: LOG:  successfully released the delegate IP:&quot;192.168.56.122&quot;</span><br><span class="line">2020-02-10 01:25:43: pid 40639: DETAIL:  &#39;if_down_cmd&#39; returned with success</span><br></pre></td></tr></table></figure>
 初步猜测是 watchdog 关闭出现了问题。目前只发现可以 ctrl+c 能干净的终止。</li>
</ol>
<h2 id="命令简介"><a href="#命令简介" class="headerlink" title="命令简介"></a>命令简介</h2><ol>
<li>pgpool<br>-C  memqcache_method 为 memcached 时，清除查询缓存<br>-n 不以守护模式运行<br>-m :<pre><code>fast: 快速关闭
smart: 关闭查询之后再关闭
immediate： 和fast类似</code></pre>-X: 打开断言检查，调试帮助<br>-d, –debug 开启debug调试</li>
<li>pcp_attach_node<br>-U, –username=NAME    username for PCP authentication<br>-h, –host=HOSTNAME    pgpool-II host<br>-p, –port=PORT        PCP port number<br>-w, –no-password      never prompt for password<br>-W, –password         强制密码认证<br>-n, –node-id=NODEID   节点ID<br>-d, –debug            开启 debug 信息<br>-v, –verbose          输出详细的信息</li>
<li>pg_ctl<br>-D  data目录，如果没有指明，默认环境变量 PGDATA。<br>-s  只打印 error<br>-t  超时时间<br>-V  版本信息<br>-w  等待直到程序完成<br>-W  不等待,也就是 -w 相反<br>-c  允许 postgres 生成核心文件<br>-l  log 文件名<br>-m :<br> fast: 直接退出<br> smart: 等待连接断开后之后再关闭<br> immediate 立即关闭，无需完全关闭，导致重启后恢复。</li>
<li>pg_rewind<br>-D, –target-pgdata=DIRECTORY  目标data目录，本地<br>   –source-pgdata=DIRECTORY  源data目录，远程<br>   –source-server=CONNSTR    连接字符串,eg:’host=192.168.56.138 port=5432 user=postgres password=d dbname=postgres’<br>-n, –dry-run                  不修改任何东西的启动<br>-P, –progress                 写进程信息<br>   –debug                    写大量debug信息</li>
</ol>
<h2 id="参考"><a href="#参考" class="headerlink" title="参考"></a>参考</h2><ol>
<li><a href="https://www.jianshu.com/p/ef183d0a9213" target="_blank" rel="noopener">https://www.jianshu.com/p/ef183d0a9213</a></li>
<li><a href="https://www.pgpool.net/docs/pgpool-II-3.5.4/doc/pgpool-zh_cn.html" target="_blank" rel="noopener">https://www.pgpool.net/docs/pgpool-II-3.5.4/doc/pgpool-zh_cn.html</a></li>
<li><a href="https://www.pgpool.net/docs/37/en/html/" target="_blank" rel="noopener">https://www.pgpool.net/docs/37/en/html/</a>      </li>
</ol>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://ddota2.gitee.io/2020/01/02/socket-readLine-%E4%BD%8E%E7%BA%A7%E9%94%99%E8%AF%AF/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/h_something/images/avatar.gif">
      <meta itemprop="name" content="Dota2 player">
      <meta itemprop="description" content="楼下的男人病的要死">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="D">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/h_something/2020/01/02/socket-readLine-%E4%BD%8E%E7%BA%A7%E9%94%99%E8%AF%AF/" class="post-title-link" itemprop="url">socket-readLine-低级错误</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-01-02 16:11:09" itemprop="dateCreated datePublished" datetime="2020-01-02T16:11:09+08:00">2020-01-02</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-11-19 16:41:30" itemprop="dateModified" datetime="2020-11-19T16:41:30+08:00">2020-11-19</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/h_something/categories/java/" itemprop="url" rel="index"><span itemprop="name">java</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <h1 id="记录一次使用关于通过netcat工具连接socket的问题吧"><a href="#记录一次使用关于通过netcat工具连接socket的问题吧" class="headerlink" title="记录一次使用关于通过netcat工具连接socket的问题吧"></a>记录一次使用关于通过netcat工具连接socket的问题吧</h1><h2 id="环境"><a href="#环境" class="headerlink" title="环境"></a>环境</h2><pre><code>本地环境
win10 企业版（2019-1206安装，奇怪不应该是专业版吗，而且我没重装系统呀，难道是上次注册表的问题？）: 
netcat: v1.12 NT
jdk: 1.8.0_112
正式环境
ubuntu: 16.04.6
netcat: OpenBSD netcat (Debian patchlevel 1.105-7ubuntu1)
jdk: 1.8.0_201-b09 </code></pre><p>在写连接设备程序时，用到了</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">String sb = bufferedReader.readLine()</span><br><span class="line">...</span><br></pre></td></tr></table></figure>
<p>程序直接部署上去，结果一夜过后发现几个T的容量直接沾满了，导致数据库挂掉了。于是查看一下是因为此程序的log写满了，觉着奇怪。删掉日志文件，重新启动，日志也没有飞速增加。<br>但是，当我使用ubuntu的nc工具连接然后断开时，发现几乎以满IO的速度写入日志。发现 bufferedReader.readLine() 不断的获取到null，然后空指针异常填满了日志。于是程序改成了</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">while</span> ((line = bufferedReader.readLine()) != <span class="keyword">null</span>) &#123;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>这样改了之后，日志确实不会继续增加了，但是发现一个新的问题，每断开一个连接，cpu就会占满一个核，使用top命令发现%CPU指数基本为（断开次数*100%），于是发现是 nc 工具ctrl+c后不会关闭socket的问题。<br>因为我外部还套了一层循环，但是在windows使用nc测试时，如果断开连接则会抛出异常Connection reset，从而跳出循环。也就不存在什么资源占用的问题。但是ubuntu的nc命令，断开连接后，socket 仍然未关闭，可以不断的重新连接，然后while true readline。<br>查看了外套的循环的作用，发现基本是while true，可能是怕读取到空值后，断开连接，于是加了层循环，若读取到null后，重新获取inputstream继续readline，找了相关资料，socket发送null，基本就是等于断开连接了。于是删除此外套的循环，问题解决。</p>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://ddota2.gitee.io/2019/12/06/windows-problem-1/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/h_something/images/avatar.gif">
      <meta itemprop="name" content="Dota2 player">
      <meta itemprop="description" content="楼下的男人病的要死">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="D">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/h_something/2019/12/06/windows-problem-1/" class="post-title-link" itemprop="url">windows_problem_1</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2019-12-06 10:56:47" itemprop="dateCreated datePublished" datetime="2019-12-06T10:56:47+08:00">2019-12-06</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-11-19 16:41:39" itemprop="dateModified" datetime="2020-11-19T16:41:39+08:00">2020-11-19</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/h_something/categories/windows/" itemprop="url" rel="index"><span itemprop="name">windows</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <h1 id="Windows-开始菜单问题"><a href="#Windows-开始菜单问题" class="headerlink" title="Windows 开始菜单问题"></a>Windows 开始菜单问题</h1><h2 id="起因："><a href="#起因：" class="headerlink" title="起因："></a>起因：</h2><p>今天由于昨天关机后自动重启，无法关机，查了下原因<br>可能是系统故障导致关机失败重启  </p>
<h2 id="解决方法一"><a href="#解决方法一" class="headerlink" title="解决方法一:"></a>解决方法一:</h2><p>右键我的电脑，点击启动与故障恢复的设置，然后取消系统失败时自动重新启动的选项。</p>
<p><img src="/h_something/images/windows-problem-1/2019-12-05-16-25-33.png" alt="a.png"></p>
<p>这个方法有那么点掩耳盗铃的味道，于是我不答应。<br>打开了 windows 的事件查看器，类似如下log，之前的被覆盖了。<br><img src="/h_something/images/windows-problem-1/2019-12-05-16-40-47.png" alt="b.log"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">计算机-默认 权限设置并未向在应用程序容器 Microsoft.Windows.Cortana_1.10.7.17134_neutral_neutral_cw5n1h2txyewy SID (S-1-15-2-1861897761-1695161497-2927542615-642690995-327840285-2659745135-2630312742)中运行的地址 LocalHost (使用 LRPC) 中的用户 D7\illus SID (S-1-5-21-1456052483-2504874860-2005770065-1001)授予针对 CLSID 为 </span><br><span class="line">&#123;D63B10C5-BB46-4990-A94F-E40B9D520160&#125;</span><br><span class="line">、APPID 为 </span><br><span class="line">&#123;9CA88EE3-ACB7-47C8-AFC4-AB702511C276&#125;</span><br><span class="line"> 的 COM 服务器应用程序的 本地 激活 权限。此安全权限可以使用组件服务管理工具进行修改。</span><br></pre></td></tr></table></figure>
<p>找了许久，要通过组件服务，修改权限才行。<br><img src="/h_something/images/windows-problem-1/2019-12-05-16-46-06.png" alt="c.png"><br>但是呢，权限是灰色的，因此又找了许久，要通过注册表修改才能获取到此完全控制权限。由于注册表的权限也不能直接修改，需要将所有者由 TrustedInstaller 改成 当前用户才能进行修改。<br><img src="/h_something/images/windows-problem-1/2019-12-05-16-54-14.png" alt=""><br>修改了所有者之后，赋予完全控制权限<br><img src="/h_something/images/windows-problem-1/2019-12-05-16-55-23.png" alt=""><br>修改 RuntimeBroker 的启动和激活权限为默认。而默认值是在下方改的<br><img src="/h_something/images/windows-problem-1/2019-12-05-17-23-33.png" alt=""><br>编辑默认值，组件服务，右键我的电脑-属性，然后编辑默认值<br>添加Network Service角色为所有权限<br><img src="/h_something/images/windows-problem-1/2019-12-05-17-25-33.png" alt=""></p>
<p>结果重启后，又出问题了，开始菜单打开后无法搜索，设置以及左侧按钮均无法点击，EDGE也是秒退<br><img src="/h_something/images/windows-problem-1/2019-12-05-18-15-04.png" alt=""><br>于是找呀找呀，<br>试过</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">管理员</span><br><span class="line"><span class="built_in">Get-AppXPackage</span> <span class="literal">-AllUsers</span> |<span class="built_in">Where-Object</span> &#123;<span class="variable">$_</span>.InstallLocation <span class="operator">-like</span> <span class="string">"*SystemApps*"</span>&#125; | <span class="keyword">Foreach</span> &#123;<span class="built_in">Add-AppxPackage</span> <span class="literal">-DisableDevelopmentMode</span> <span class="literal">-Register</span> <span class="string">"<span class="variable">$</span>(<span class="variable">$_</span>.InstallLocation)\AppXManifest.xml"</span>&#125;</span><br><span class="line">sfc /scannow</span><br></pre></td></tr></table></figure>
<p>也试过</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">任务管理器运行</span><br><span class="line"><span class="variable">$manifest</span> = (<span class="built_in">Get-AppxPackage</span> Microsoft.WindowsStore).InstallLocation + <span class="string">'\AppxManifest.xml'</span> ; <span class="built_in">Add-AppxPackage</span> <span class="literal">-DisableDevelopmentMode</span> <span class="literal">-Register</span> <span class="variable">$manifest</span></span><br></pre></td></tr></table></figure>
<p>以及好多好多，均无作用。<br>最后倒入了很久之前的注册表，已经刚出问题时的注册表，导入时还都报错(访问注册表时出错)，重启莫名好了。奇怪</p>
<p>后来设置为默认值重现了一次，后续改成自定义，打补丁，导入最新注册表都没用。只能导入以前的注册表才生效了</p>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  


  
  <nav class="pagination">
    <span class="page-number current">1</span><a class="page-number" href="/h_something/page/2/">2</a><a class="extend next" rel="next" href="/h_something/page/2/"><i class="fa fa-angle-right" aria-label="下一页"></i></a>
  </nav>



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">Dota2 player</p>
  <div class="site-description" itemprop="description">楼下的男人病的要死</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/h_something/archives/">
        
          <span class="site-state-item-count">11</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/h_something/categories/">
          
        <span class="site-state-item-count">5</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/h_something/tags/">
          
        <span class="site-state-item-count">15</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Dota2 player</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/h_something/lib/anime.min.js"></script>
  <script src="/h_something/lib/velocity/velocity.min.js"></script>
  <script src="/h_something/lib/velocity/velocity.ui.min.js"></script>

<script src="/h_something/js/utils.js"></script>

<script src="/h_something/js/motion.js"></script>


<script src="/h_something/js/schemes/muse.js"></script>


<script src="/h_something/js/next-boot.js"></script>




  















  

  

</body>
</html>
